import static org.junit.Assert.*;

import java.io.File;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import svgedit.svg.SVGCircleElement;
import svgedit.svg.SVGDocument;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGGroup;
import svgedit.svg.SVGLineElement;
import svgedit.svg.SVGRectElement;
import svgedit.xml.XMLReader;

/**
 * This test unit verified that the editors general commands are working
 * 
 * @author SARJ
 * 
 */
public class EditorTests
{

	private static SVGGroup root;
	private static SVGDocument document;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{

	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
	}

	@Before
	public void setUp() throws Exception
	{
		File f = new File("tests/simpletest.svg");
		SVGDocument doc = XMLReader.read(f, null);
		document = doc;
		root = doc.getRootGroup();
	}

	@After
	public void tearDown() throws Exception
	{
		document = null;
		root = null;
	}

	@Test
	public void testDeleteElement()
	{

		SVGElement elem = null;

		for (SVGElement e : root.getChildren())
		{
			if (!(e instanceof SVGGroup))
			{
				elem = e;
				break;
			}
		}

		// Delete the element
		elem.getParent().removeChild(elem);

		// Check
		boolean found = false;
		for (SVGElement e : root.getChildren())
		{
			if (elem.equals(e))
				found = true;
		}

		// Should not find
		assertFalse(found);

	}

	@Test
	public void testDeleteGroup()
	{

		SVGElement elem = null;

		for (SVGElement e : root.getChildren())
		{
			if (e instanceof SVGGroup)
			{
				elem = e;
				break;
			}
		}

		// Delete the group
		elem.getParent().removeChild(elem);

		// Check
		boolean found = false;
		for (SVGElement e : root.getChildren())
		{
			if (elem.equals(e))
				found = true;
		}

		// Should not find
		assertFalse(found);

	}

	@Test
	public void testInsertCircle()
	{

		SVGCircleElement circle = new SVGCircleElement(document);

		// Add circle
		root.appendChild(circle);

		// Find it
		boolean found = false;
		for (SVGElement e : root.getChildren())
		{
			if (e.equals(circle))
			{
				found = true;
				break;
			}
		}

		assertTrue(found);
	}

	@Test
	public void testInsertLine()
	{

		SVGLineElement line = new SVGLineElement(document);

		// Add line
		root.appendChild(line);

		// Find it
		boolean found = false;
		for (SVGElement e : root.getChildren())
		{
			if (e.equals(line))
			{
				found = true;
				break;
			}
		}

		assertTrue(found);
	}

	@Test
	public void testInsertRect()
	{

		SVGRectElement rect = new SVGRectElement(document);

		// Add line
		root.appendChild(rect);

		// Find it
		boolean found = false;
		for (SVGElement e : root.getChildren())
		{
			if (e.equals(rect))
			{
				found = true;
				break;
			}
		}

		assertTrue(found);
	}

}
